/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.dao.hibernate;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import unilearn.dao.SubjectDAO;
import unilearn.vo.Subject;

/**
 *
 * @author Yat
 */
public class SubjectDAOImpl extends HibernateDAO implements SubjectDAO {

    public SubjectDAOImpl() {
        super();
    }

    public SubjectDAOImpl(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<Subject> getAll() throws Exception {
        return super.getAll(Subject.class.getName());
    }

    @Override
    public Subject get(String subjectCode) throws Exception {
        Session s = super.sessionFactory.openSession();
        Transaction t = s.beginTransaction();

        try {
            Query query = s.createQuery("from " + Subject.class.getName() + " s where s.subjectCode='" + subjectCode + "'");
            List<Subject> ss = query.list();
            if ((ss == null) || (ss.isEmpty())) {
                return null;
            }

            return ss.get(0);
        } finally {
            t.commit();
            s.close();
        }
    }

    @Override
    public List<Subject> find(Subject subject) throws Exception {
        return (List<Subject>) super.find(subject);
    }

    @Override
    public Subject insert(Subject subject) throws Exception {
        super.insert(subject);
        return subject;
    }

    @Override
    public Subject update(Subject subject) throws Exception {
        super.update(subject);
        return subject;
    }

    @Override
    public void delete(Subject subject) throws Exception {
        super.delete(subject);
    }
}
